package service

import (
	"fmt"
	"zhonghui/console/warehouse/cfgx"

	"github.com/ddliu/go-httpclient"
	jsoniter "github.com/json-iterator/go"
)

type ExternalContact struct {
	Avatar         string `json:"avatar"`
	CorpFullName   string `json:"corp_full_name"`
	CorpName       string `json:"corp_name"`
	ExternalUserid string `json:"external_userid"`
	Gender         int32  `json:"gender"`
	Name           string `json:"name"`
	Position       string `json:"position"`
	Type           int32  `json:"type"`
	Unionid        string `json:"unionid"`
}

type CustomerDetailResponse struct {
	Errcode         int64           `json:"errcode"`
	Errmsg          string          `json:"errmsg"`
	ExternalContact ExternalContact `json:"external_contact"`
}

func GetCustomerDetail(AccessToken, externalUserID string) (*CustomerDetailResponse, error) {
	resp := CustomerDetailResponse{}
	host := cfgx.Cfg().GetString("wx.host")
	url := fmt.Sprintf("%s/cgi-bin/externalcontact/get", host)

	res, err := httpclient.Get(url, map[string]string{
		"access_token":    AccessToken,
		"external_userid": externalUserID,
	})
	if err != nil {
		return &resp, err
	}
	ret, err := res.ToString()
	if err != nil {
		return &resp, err
	}
	return &resp, jsoniter.UnmarshalFromString(ret, &resp)
}
